{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "501a60f9-8f89-4ef2-9542-169fda51df33",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index.tools.tool_spec.notion.base import NotionToolSpec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f63d8382-3015-466a-857e-782c31b2128a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "notion_token = \"\"\n",
    "\n",
    "tool_spec = NotionToolSpec(integration_token=notion_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e0985d0a-ff3e-44d0-88d6-b966d92151e5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# get tool\n",
    "tools = tool_spec.to_tool_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dbc0d3a7-f43d-4851-bbc1-20ea4c7f2e71",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "lc_tools = [\n",
    "    t.to_langchain_structured_tool(return_direct=False, verbose=True) for t in tools\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "df5e7e26-89d9-4ba9-8183-5596d757626d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.agents import initialize_agent\n",
    "from langchain.chat_models import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b73d5128-8545-4737-8ff4-982f5a8e950c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(temperature=0, model_name=\"gpt-3.5-turbo\", streaming=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "35259859-a442-4b4b-a3cc-1f6c1ab9c8b1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "agent = initialize_agent(\n",
    "    lc_tools, llm=llm, agent=\"structured-chat-zero-shot-react-description\", verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8b286e1c-60c8-46c6-a501-2b892f1065c9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mAction:\n",
      "```\n",
      "{\n",
      "  \"action\": \"search_data\",\n",
      "  \"action_input\": {\n",
      "    \"query\": \"Worked On\"\n",
      "  }\n",
      "}\n",
      "```\n",
      "\n",
      "\u001b[0m\n",
      "Observation: \u001b[33;1m\u001b[1;3m[{'object': 'page', 'id': '4ffc9d63-3825-4315-ae6f-b1e6f8bc033f', 'created_time': '2022-12-02T03:58:00.000Z', 'last_edited_time': '2023-01-29T01:32:00.000Z', 'created_by': {'object': 'user', 'id': '99fdef27-306b-4559-98fd-f9601429301d'}, 'last_edited_by': {'object': 'user', 'id': '99fdef27-306b-4559-98fd-f9601429301d'}, 'cover': None, 'icon': None, 'parent': {'type': 'workspace', 'workspace': True}, 'archived': False, 'properties': {'title': {'id': 'title', 'type': 'title', 'title': [{'type': 'text', 'text': {'content': 'What I Worked On (test)', 'link': None}, 'annotations': {'bold': False, 'italic': False, 'strikethrough': False, 'underline': False, 'code': False, 'color': 'default'}, 'plain_text': 'What I Worked On (test)', 'href': None}]}}, 'url': 'https://www.notion.so/What-I-Worked-On-test-4ffc9d6338254315ae6fb1e6f8bc033f'}, {'object': 'page', 'id': '02a31f08-60cb-43d0-bdc5-b5d1b7afbf38', 'created_time': '2023-01-23T19:41:00.000Z', 'last_edited_time': '2023-01-23T19:41:00.000Z', 'created_by': {'object': 'user', 'id': '99fdef27-306b-4559-98fd-f9601429301d'}, 'last_edited_by': {'object': 'user', 'id': '99fdef27-306b-4559-98fd-f9601429301d'}, 'cover': None, 'icon': None, 'parent': {'type': 'database_id', 'database_id': '72f1031c-d941-47a1-8e25-87b4e3ae1d64'}, 'archived': False, 'properties': {'Tags': {'id': '%5Cr%5BI', 'type': 'multi_select', 'multi_select': []}, 'Name': {'id': 'title', 'type': 'title', 'title': [{'type': 'text', 'text': {'content': 'What I Worked On', 'link': None}, 'annotations': {'bold': False, 'italic': False, 'strikethrough': False, 'underline': False, 'code': False, 'color': 'default'}, 'plain_text': 'What I Worked On', 'href': None}]}}, 'url': 'https://www.notion.so/What-I-Worked-On-02a31f0860cb43d0bdc5b5d1b7afbf38'}, {'object': 'page', 'id': 'cdabeb01-95a6-4b56-ac50-f25fc4795d3e', 'created_time': '2022-12-02T03:10:00.000Z', 'last_edited_time': '2023-01-15T19:56:00.000Z', 'created_by': {'object': 'user', 'id': '99fdef27-306b-4559-98fd-f9601429301d'}, 'last_edited_by': {'object': 'user', 'id': '99fdef27-306b-4559-98fd-f9601429301d'}, 'cover': None, 'icon': None, 'parent': {'type': 'workspace', 'workspace': True}, 'archived': False, 'properties': {'title': {'id': 'title', 'type': 'title', 'title': [{'type': 'text', 'text': {'content': 'What I Worked On', 'link': None}, 'annotations': {'bold': False, 'italic': False, 'strikethrough': False, 'underline': False, 'code': False, 'color': 'default'}, 'plain_text': 'What I Worked On', 'href': None}]}}, 'url': 'https://www.notion.so/What-I-Worked-On-cdabeb0195a64b56ac50f25fc4795d3e'}]\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mThe search_data tool returned a list of pages that contain the phrase \"Worked On\". The first page in the list has the title \"What I Worked On (test)\". To get the contents of this page, I can use the load_data tool with the page id '4ffc9d63-3825-4315-ae6f-b1e6f8bc033f'.\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"load_data\",\n",
      "  \"action_input\": {\n",
      "    \"page_ids\": [\"4ffc9d63-3825-4315-ae6f-b1e6f8bc033f\"]\n",
      "  }\n",
      "}\n",
      "```\n",
      "\n",
      "\n",
      "\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3mBefore college the two main things I worked on, outside of school, were writing and programming. I didn't write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined made them deep.\n",
      "\tThe first programs I tried writing were on the IBM 1401 that our school district used for what was then called \"data processing.\" This was in 9th grade, so I was 13 or 14. The school district's 1401 happened to be in the basement of our junior high school, and my friend Rich Draves and I got permission to use it. It was like a mini Bond villain's lair down there, with all these alien-looking machines — CPU, disk drives, printer, card reader — sitting up on a raised floor under bright fluorescent lights.\n",
      "\t\tThe language we used was an early version of Fortran. You had to type programs on punch cards, then stack them in the card reader and press a button to load the program into memory and run it. The result would ordinarily be to print something on the spectacularly loud printer.\n",
      "\n",
      "\n",
      "\thello world\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mThe load_data tool returned the contents of the page with the title \"What I Worked On (test)\" and the page id '4ffc9d63-3825-4315-ae6f-b1e6f8bc033f'. The contents include a personal anecdote about the human's experience with writing and programming, as well as a snippet of code for a \"hello world\" program. I will provide this information as the final response to the human.\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"The page with the title 'What I Worked On (test)' contains a personal anecdote about your experience with writing and programming, as well as a snippet of code for a 'hello world' program.\"\n",
      "}\n",
      "```\n",
      "\n",
      "\n",
      "\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"The page with the title 'What I Worked On (test)' contains a personal anecdote about your experience with writing and programming, as well as a snippet of code for a 'hello world' program.\""
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.run(\"Search list of pages for 'Worked On' and get the page contents\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1b6b29e-660a-4710-b941-305af0ca39f2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_index_v2",
   "language": "python",
   "name": "llama_index_v2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
